//
//  MBNotificationView3.m
//  Meboo
//
//  Created by kyo on 2023/10/21.
//

#import "MBNotificationView3.h"
#import "NSConversationViewController.h"
@interface MBNotificationView3 ()
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UIView *purpleIconView;
@property (nonatomic, strong) UIView *whiteIconView;
@property (nonatomic, strong) UIImageView *iconImageView;


@property (nonatomic, strong) UIImageView *sendBackImageView;
@property (nonatomic, strong) UIView *whiteSendView;
@property (nonatomic, strong) UIImageView *sendImageView;

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;


@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIImageView *secretImageView;
@property (nonatomic, strong) UIImageView *playImageView;

@property (nonatomic, copy) NSString *targetId;
@end

@implementation MBNotificationView3

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Public
- (void)setDict:(NSMutableDictionary *)dict {
    self.titleLab.text = [NSString stringWithFormat:@"%@",dict[@"title"]];
    self.subTitleLab.text = [NSString stringWithFormat:@"%@",dict[@"body"]];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dict[@"image"]]]];
    if (dict[@"imageCache"]) {
        self.sendImageView = dict[@"imageCache"];
    } else {
        [self.sendImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dict[@"imageUrl"]]]];
    }
    self.targetId = [NSString stringWithFormat:@"%@",dict[@"targetId"]];
}

- (void)setIsBlurred:(BOOL)isBlurred {
    self.effectView.hidden = !isBlurred;
    self.secretImageView.hidden = !isBlurred;
    [self.playImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.sendImageView).offset(MBWidthScale(-5));
        make.bottom.equalTo(self.sendImageView).offset(MBWidthScale(-5));
        make.width.height.mas_equalTo(MBHeightScale(9));
    }];
}

#pragma mark - Action
- (void)swipeAction {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

- (void)clickAction {
    if (self.clickBlock) {
        self.clickBlock();
    }
    NSConversationViewController *conversationVC = [[NSConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:self.targetId];
    conversationVC.isSayHi = YES;
    [self.currentViewController.navigationController pushViewController:conversationVC animated:YES];
}

#pragma mark - UI
- (void)setupUI {
    UISwipeGestureRecognizer* swipeDirection = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];
   /*设置滑动方向*/
    swipeDirection.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:swipeDirection];
    
    UITapGestureRecognizer *actionTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction)];
    [self addGestureRecognizer:actionTap];
    
    
    self.layer.cornerRadius = MBHeightScale(12);
    
    [self addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self addSubview:self.purpleIconView];
    [self.purpleIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(15));
        make.centerY.equalTo(self);
        make.width.height.mas_equalTo(MBHeightScale(49));
    }];
    
    [self addSubview:self.whiteIconView];
    [self.whiteIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.purpleIconView);
        make.width.height.mas_equalTo(MBHeightScale(47));
    }];
    
    [self addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.purpleIconView);
        make.width.height.mas_equalTo(MBHeightScale(43));
    }];
    
    
    
    [self addSubview:self.sendBackImageView];
    [self.sendBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-20));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(MBWidthScale(48));
        make.height.mas_equalTo(MBHeightScale(64));
    }];
    
    [self addSubview:self.sendImageView];
    [self.sendImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-30));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(MBWidthScale(45));
        make.height.mas_equalTo(MBHeightScale(60));
    }];
    [self.sendImageView layoutIfNeeded];
    
    [self.sendImageView addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.sendImageView);
    }];
    
    [self addSubview:self.secretImageView];
    [self.secretImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.sendImageView);
    }];
    
    
    [self addSubview:self.playImageView];
    [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.sendImageView);
        make.width.height.mas_equalTo(MBHeightScale(18));
    }];
    
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.purpleIconView.mas_right).offset(MBWidthScale(14));
        make.top.equalTo(self.purpleIconView).offset(MBHeightScale(-2));
    }];
    
    [self addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLab);
        make.right.equalTo(self.sendImageView.mas_left).offset(MBWidthScale(-8));
        make.top.equalTo(self.titleLab.mas_bottom).offset(MBHeightScale(4));
    }];

}

#pragma mark - Setter && Getter
- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.contentMode = UIViewContentModeScaleToFill;
        _backImageView.image = ImageWithName(@"noti_back_1");
        _backImageView.layer.cornerRadius = MBHeightScale(12);
        _backImageView.layer.masksToBounds = YES;
    }
    return _backImageView;
}

- (UIView *)purpleIconView {
    if (!_purpleIconView) {
        _purpleIconView = [UIView new];
        _purpleIconView.backgroundColor = HEXS(@"A216DE");
        _purpleIconView.layer.cornerRadius = MBHeightScale(49.0/2);
    }
    return _purpleIconView;
}

- (UIView *)whiteIconView {
    if (!_whiteIconView) {
        _whiteIconView = [UIView new];
        _whiteIconView.backgroundColor = HEXS(@"ffffff");
        _whiteIconView.layer.cornerRadius = MBHeightScale(47.0/2);
    }
    return _whiteIconView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.layer.cornerRadius = MBHeightScale(43.0/2);
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}


//右侧图
- (UIImageView *)sendBackImageView {
    if (!_sendBackImageView) {
        _sendBackImageView = [UIImageView new];
        _sendBackImageView.image = ImageWithName(@"noti_right_back");
    }
    return _sendBackImageView;
}

//- (UIView *)whiteIconView {
//    if (!_whiteIconView) {
//        _whiteIconView = [UIView new];
//        _whiteIconView.backgroundColor = HEXS(@"ffffff");
//        _whiteIconView.layer.cornerRadius = MBHeightScale(47.0/2);
//    }
//    return _whiteIconView;
//}

- (UIImageView *)sendImageView {
    if (!_sendImageView) {
        _sendImageView = [UIImageView new];
        _sendImageView.contentMode = UIViewContentModeScaleAspectFill;
        _sendImageView.layer.borderWidth = 2;
        _sendImageView.layer.borderColor = UIColor.whiteColor.CGColor;
        _sendImageView.layer.cornerRadius = MBHeightScale(8);
        _sendImageView.layer.masksToBounds = YES;
    }
    return _sendImageView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = PDSemiBoldScale(14);
        _titleLab.textColor = HEXS(@"080808");
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [[UILabel alloc] init];
        _subTitleLab.numberOfLines = 2;
        _subTitleLab.font = PDRegularScale(12);
        _subTitleLab.textColor = HEXS(@"84868E");
    }
    return _subTitleLab;
}

- (UIVisualEffectView *)effectView {
    if (!_effectView) {
        UIBlurEffect *blurEffrct = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
        _effectView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.4];
        _effectView.alpha = 0.70;
        _effectView.hidden = YES;
    }
    return _effectView;
}

- (UIImageView *)secretImageView {
    if (!_secretImageView) {
        _secretImageView = [UIImageView new];
        _secretImageView.hidden = YES;
        _secretImageView.image = ImageWithName(@"noti_right_secret");
    }
    return _secretImageView;
}

- (UIImageView *)playImageView {
    if (!_playImageView) {
        _playImageView = [UIImageView new];
        _playImageView.image = ImageWithName(@"noti_right_video_play");
    }
    return _playImageView;
}
@end
